package com.culturalCenter.dataCenter.service.impl.activity;

import com.culturalCenter.dataCenter.Utils.DateFormatUtil;
import com.culturalCenter.dataCenter.entity.activity.ActivityServedNumbers;
import com.culturalCenter.dataCenter.mapper.activity.ActivityServedNumberMapper;
import com.culturalCenter.dataCenter.mapper.training.TrainingMapper;
import com.culturalCenter.dataCenter.param.ServiceVolumeParam;
import com.culturalCenter.dataCenter.service.activity.ActivityServedNumberService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
*
*/
@Service
public class ActivityServedNumberServiceImpl implements ActivityServedNumberService {
    @Resource
    private ActivityServedNumberMapper activityServedNumberMapper;
    @Resource
    private TrainingMapper trainingMapper;

    @Override
    public String insertServerNumber(String branchId, String date, int onLineNumber, int offLineNumber) {
        ActivityServedNumbers activityServedNumbers = new ActivityServedNumbers();
        activityServedNumbers.setBranchid("462673814262349824");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date today = null;
        try {
            today = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        activityServedNumbers.setDate(today);
        activityServedNumbers.setOnlinenumber(onLineNumber);
        activityServedNumbers.setOfflinenumber(offLineNumber);
        ActivityServedNumbers one = activityServedNumberMapper.findOne(today);
        if (one==null){

            int insert = activityServedNumberMapper.insert(activityServedNumbers);
            return insert == 1?"success":"fail";
        }else {
            int update = activityServedNumberMapper.update(activityServedNumbers);
            return update == 1?"success":"fail";
        }
    }

    @Override
    public void addServiceVolume(ServiceVolumeParam param) {
        if (StringUtils.isBlank(param.getDate()))
            param.setDate(DateFormatUtil.format(new Date()));
        switch (param.getType()){
            case "activity" :
                activityServedNumberMapper.addServiceVolume(param);
                break;
            case "training" :
                trainingMapper.addServiceVolume(param);
                break;
        }
    }
}
